<template>
    <div class="security">
        <Header></Header>
        <el-row class="content">
            <el-col :span="4" class="left">
                <div class="logo tc">
                    <img src="@/assets/images/security_logo.png" alt="logo">
                </div>
                <div class="area-list">
                    <ul>
                        <li :class="{active: index === clickIndex}"
                            v-for="(item, index) in areaList"
                            @click="listHandle(index,item)">
                            {{ item.areaName }}
                        </li>
                    </ul>
                </div>
            </el-col>
            <el-col :span="20" class="right">
                <div class="head">
                    <div class="head-left fl">
                        <img class="icon-logo fl" src="@/assets/images/security_icon.png" alt="icon">
                        <ul class="fl">
                            <li class="bold">一楼会议室</li>
                            <li>开启报警后，固定资产离开所在区域即报警</li>
                        </ul>
                    </div>
                    <div class="head-right fr tr">
                        <div class="input fl">
                            <input type="text" placeholder="固定资产名称" v-model="searchParam">
                            <i @click="search"></i>
                        </div>
                        <ul class="circle">
                            <li>
                                <div class="asset-circle">
                                    <span>{{ assetsMsg.totalAssetNum }}</span>
                                </div>
                                <p>固定资产数</p>
                            </li>
                            <li>
                                <div class="warn-circle">
                                    <span>{{ assetsMsg.alarmOpenNum }}</span>
                                </div>
                                <p>开启报警数</p>
                            </li>
                        </ul>
                    </div>
                </div>
                <div class="body">
                    <div class="segment"></div>
                    <el-row>
                        <el-col :span="18" class="container">
                            <div class="segment fr"></div>
                            <div class="list warning-asset">
                                <p class="title">已开启报警的固定资产</p>
                                <ul class="asset-list">
                                    <li v-for="(item,index) in readyWarn">
                                        <p>{{ item.name }}</p>
                                        <p class="number">{{ item.id }}</p>
                                        <div class="banner tc" @click="readyWarnClose(index,item)">点击关闭</div>
                                    </li>
                                </ul>
                            </div>
                        </el-col>
                        <el-col :span="6" class="container">
                            <div class="list not-warning-asset">
                                <p class="title">未开启报警的固定资产</p>
                                <ul class="asset-list">
                                    <li v-for="(item,index) in notReadyWarn">
                                        <p>{{ item.name }}</p>
                                        <p class="number">{{ item.id }}</p>
                                        <div class="banner tc" @click="notReadyWarnOpen(index,item)">点击开启</div>
                                    </li>
                                </ul>
                            </div>
                        </el-col>
                    </el-row>
                </div>
            </el-col>
        </el-row>
    </div>
</template>

<script>
    import Header from '@/components/header'

    export default {
        name: "security",
        components: {
            Header
        },
        data() {
            return {
                clickIndex: -1,
                areaList: [], // 左侧区域列表
                assetsMsg: {},
                readyWarn: [],
                notReadyWarn: [],
                searchParam: '', // 搜索框参数
            }
        },
        methods: {
            listHandle(i, item) {
                this.clickIndex = i
                let params = {
                    areaId: item.areaId,
                    pageIndex: 1,
                    pageSize: 1000
                }
                if (localStorage.getItem("user")) {
                    params.userId = JSON.parse(localStorage.getItem("user")).userId
                }
                this.$ajax
                    .assets
                    .getAssetsByAreaId(params)
                    .then((res) => {
                        let result = res.data
                        if (result.code === 200) {
                            if (result.result) {
                                let assets = result.result.content
                                this.getWarnAsset(assets)
                            }
                        } else {
                            this.$message.error(result.msg)
                        }
                    }, () => {
                        this.$message.error(this.$msgDictionary.serviceException)
                    })
            },
            readyWarnClose(i, item) {
                this.$alert('确定关闭吗？', '提示', {
                    confirmButtonText: '确定',
                    center: true,
                    closeOnClickModal: true,
                    confirmButtonClass: 'dialog-big-button',
                }).then(() => {
                    this.readyWarn.splice(i, 1)
                    this.notReadyWarn.push(item)
                    this.alarmSwitch(item)
                }).catch(() => {

                })
            },
            notReadyWarnOpen(i, item) {
                this.$alert('确定关闭吗？', '提示', {
                    confirmButtonText: '确定',
                    center: true,
                    closeOnClickModal: true,
                    confirmButtonClass: 'dialog-big-button',
                }).then(() => {
                    this.alarmSwitch(i, item)
                }).catch(() => {

                })
            },
            /**
             * 报警开关
             */
            alarmSwitch(i, row) {
                let params = {
                    assetsId: row.id,
                    userId: "",
                    alarmSwitch: !row.alarmSwitch
                }
                if (localStorage.getItem("user")) {
                    params.userId = JSON.parse(localStorage.getItem("user")).userId
                }
                console.log(params)
                this.$ajax
                    .assets
                    .alarmSwitch(params)
                    .then((res) => {
                        let result = res.data
                        if (result.code === 200) {
                            this.notReadyWarn.splice(i, 1)
                            this.readyWarn.push(row)
                            this.$message.success(this.$msgDictionary.operation.suc)
                        } else {
                            this.$message.error(result.msg)
                            row.alarmSwitch = !params.alarmSwitch
                        }
                    }, () => {
                        this.$message.error(this.$msgDictionary.serviceException)
                    })
            },
            /**
             * 获取区域信息
             * @param params
             */
            getAreas(params) {
                this.$ajax
                    .areas
                    .getAreas(params)
                    .then((res) => {
                        let result = res.data
                        if (result.code === 200) {
                            if (result.result) {
                                this.areaList = result.result.areasInfoDTOS.content
                            }
                        } else if (result.code === 408) {
                            this.$message.error(this.$msgDictionary.loginTimeout)
                            let timer = setTimeout(() => {
                                clearTimeout(timer)
                                this.$router.push({path: '/login'})
                            }, 1500)
                        } else {
                            this.$message.error(result.msg)
                        }
                    }, () => {
                        this.$message.error(this.$msgDictionary.serviceException)
                    })
            },
            /**
             * 获取固定资产信息
             * @param params
             */
            getAssets(params) {
                this.$ajax
                    .assets
                    .getAssets(params)
                    .then((res) => {
                        let result = res.data
                        if (result.code === 200) {
                            if (result.result) {
                                this.assetsMsg = result.result
                                if (this.assetsMsg.assetsInfoPage) {
                                    let assets = this.assetsMsg.assetsInfoPage.content
                                    this.getWarnAsset(assets)
                                }
                            }
                        } else if (result.code === 408) {
                            this.$message.error(this.$msgDictionary.loginTimeout)
                            let timer = setTimeout(() => {
                                clearTimeout(timer)
                                this.$router.push({path: '/login'})
                            }, 1500)
                        } else {
                            this.$message.error(result.msg)
                        }
                    }, () => {
                        this.$message.error(this.$msgDictionary.serviceException)
                    })
            },
            /**
             * 分离报警设备和未报警设备
             */
            getWarnAsset(assets) {
                this.readyWarn.length = 0
                this.notReadyWarn.length = 0
                assets.map(item => {
                    if (item.alarmSwitch) {
                        this.readyWarn.push(item)
                    } else {
                        this.notReadyWarn.push(item)
                    }
                })
            },
            /**
             * 搜索
             */
            search() {
                let params = {
                    userId: "",
                    pageSize: 20,
                    pageIndex: 1
                }
                if (localStorage.getItem("user")) {
                    params.userId = JSON.parse(localStorage.getItem("user")).userId
                }
                if (this.searchParam.trim() === '') {
                    this.getAssets(params)
                    this.$message.error(this.$msgDictionary.query.emptyInput)
                    return
                }
                params.name = this.searchParam.trim()
                this.$ajax
                    .assets
                    .getAssetsByName(params)
                    .then((res) => {
                        let result = res.data
                        if (result.code === 200) {
                            if (result.result.content.length === 0) {
                                this.$message.error(this.$msgDictionary.query.emptyResult)
                            } else {
                                this.getWarnAsset(result.result.content)
                            }
                        } else {
                            this.$message.error(result.msg)
                        }
                    }, () => {
                        this.$message.error(this.$msgDictionary.serviceException)
                    })
            }
        },
        mounted() {
            let params = {
                userId: "",
                pageSize: 20,
                pageIndex: 1
            }
            if (localStorage.getItem("user")) {
                params.userId = JSON.parse(localStorage.getItem("user")).userId
            }
            this.getAreas(params)
            this.getAssets(params)
        }
    }
</script>

<style lang="scss" type="text/scss" scoped>
    @import "../assets/css/color";

    $segment_left: #EFDFDF;
    $segment_right: #F1ECEC;
    $red: #E53B0A;
    $yellow: #FFA43B;

    .content {
        height: 90vh;
        .left {
            height: 90vh;
            background: $bg_black;
            .logo {
                height: 20vh;
                line-height: 20vh;
                background: $title_black;
                img {
                    width: 1rem;
                    height: 0.9rem;
                }
            }
            .area-list {
                padding: 10% 0;
                height: 70vh;
                box-sizing: border-box;
                ul {
                    height: 100%;
                    overflow: auto;
                    white-space: nowrap;
                    li {
                        padding-left: 1rem;
                        border-left: 0.05rem solid transparent;
                        height: 0.4rem;
                        line-height: 0.4rem;
                        font-size: 0.16rem;
                        color: $white;
                        cursor: pointer;
                        margin-bottom: 0.1rem;
                        overflow: hidden;
                        text-overflow: ellipsis;
                        &.active {
                            border-color: $strong_red;
                            color: $strong_red;
                        }
                    }
                }
            }
        }
        .right {
            height: 90vh;
            .head {
                height: 20vh;
                padding: 0.3rem;
                box-sizing: border-box;
                .head-left {
                    width: 30%;
                    white-space: nowrap;
                    margin-top: 0.3rem;
                    .icon-logo {
                        width: 0.66rem;
                        height: 0.59rem;
                        margin-right: 0.12rem;
                    }
                    li {
                        font-size: 0.18rem;
                        color: $ph_color;
                        &.bold {
                            font-size: 0.24rem;
                            color: $black;
                            margin-bottom: 0.2rem;
                        }
                    }
                }
                .head-right {
                    width: 40%;
                    padding-top: 0.1rem;
                    .input {
                        display: inline-block;
                        border: 1px solid $segment;
                        width: 3rem;
                        height: 0.5rem;
                        line-height: 0.5rem;
                        border-radius: 0.25rem;
                        text-align: left;
                        white-space: nowrap;
                        margin-top: 0.25rem;
                        position: relative;
                        input {
                            height: 100%;
                            width: 80%;
                            padding-left: 0.2rem;
                            outline: none;
                            box-sizing: border-box;
                            vertical-align: top;
                            border: 0;
                            background: transparent;
                        }
                        i {
                            display: inline-block;
                            width: 33px;
                            height: 34px;
                            position: absolute;
                            top: 0.07rem;
                            right: 0.14rem;
                            cursor: pointer;
                            float: right;
                            background: url("../assets/images/spirit.png") no-repeat -399px -249px;
                        }
                    }
                    .circle {
                        display: inline-block;
                        li {
                            float: left;
                            margin: 0 0.3rem;
                            div {
                                border: 1px solid $red;
                                height: 0.7rem;
                                width: 0.7rem;
                                line-height: 0.7rem;
                                border-radius: 50%;
                                text-align: center;
                                span {
                                    font-size: 0.24rem;
                                }
                                &.asset-circle {
                                    span {
                                        color: $red;
                                    }
                                }
                                &.warn-circle {
                                    border-color: $yellow;
                                    span {
                                        color: $yellow;
                                    }
                                }
                            }
                            p {
                                padding-top: 0.2rem;
                                color: $ph_color;
                            }
                        }
                    }
                }
            }
            .body {
                height: 70vh;
                .segment {
                    height: 1.5vh;
                    background: linear-gradient(to bottom, $segment_left, $segment_right);
                }
                .container {
                    height: 68.5vh;
                    .segment {
                        width: 0.1rem;
                        height: 100%;
                    }
                    .list {
                        padding: 0.3rem;
                        padding-right: 0;
                        .title {
                            font-size: 0.2rem;
                            margin: 0.2rem 0;
                        }
                        .asset-list {
                            height: 5rem;
                            overflow: auto;
                            li {
                                width: 1.5rem;
                                height: 0.8rem;
                                float: left;
                                border: 1px solid $strong_red;
                                margin-right: 0.2rem;
                                margin-bottom: 0.2rem;
                                padding: 0.25rem;
                                box-sizing: border-box;
                                overflow: hidden;
                                position: relative;
                                box-shadow: 5px 5px 20px 0 #F9EAE5;
                                p {
                                    font-size: 0.16rem;
                                    white-space: nowrap;
                                    overflow: hidden;
                                    text-overflow: ellipsis;
                                    &.number {
                                        color: $ph_color;
                                        padding-top: 0.04rem;
                                        font-size: 0.14rem;
                                    }
                                }
                                .banner {
                                    position: absolute;
                                    bottom: -0.3rem;
                                    left: 0;
                                    transition: bottom 0.1s linear;
                                    height: 0.3rem;
                                    line-height: 0.3rem;;
                                    width: 100%;
                                    font-size: 0.16rem;
                                    color: $white;
                                    cursor: pointer;
                                    background: linear-gradient(to right, $guide_gradient_b, $guide_gradient_e);
                                }
                                &:hover {
                                    .banner {
                                        bottom: 0;
                                    }
                                }
                            }
                        }
                        &.not-warning-asset {
                            li {
                                border-color: $yellow;
                                box-shadow: 5px 5px 20px 0 #F9EAE5;
                                .banner {
                                    background: linear-gradient(to right, $assets_gradient_b, $assets_gradient_e);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
</style>
